package org.lex.lang;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.lex.utils.Result;


public class Invoker {
	private Invoker() {
	}

	public static <T> Object invokeQuietly(T var, String methodName, Object... args) {
		try {
			return invoke(var, methodName, args);
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}

	public static <T> Object invoke(T var, String methodName, Object... args) throws SecurityException, NoSuchMethodException,
			IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		Class<?> varClass = var.getClass();
		Class<?>[] argsClass = null;
		if (null == args || 0 == args.length)
			argsClass = new Class[0];
		else {
			argsClass = new Class[args.length];
			for (int i = 0; i < args.length; i++)
				argsClass[i] = args[i].getClass();
		}
		Method method = varClass.getMethod(methodName, argsClass);
		return method.invoke(var, args);
	}
}
